module.exports = {
	/**
	 * 修改数据
	 * @url admin/kong/sys/update 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @param {Number} code 错误码，0表示成功
	 * @param {String} msg 详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: 'ok' };
		// 业务逻辑开始-----------------------------------------------------------
		let {
			_id,
			client,
			client_name,
			detection_type,
			submitUser,
			detection_category,
			detection_standard,
			detection_standard_name,
			detection_user,
			detect_time,
					sample_id,
			sampleno,
			samplename,
			sample_save_info,
			sample_keeptime,
			sample_troptime,
			type,//是否溯源报告
			sample_status,
			sample_list
		} = data;
		// 这里需要把 params1 params2 params3 改成你数据库里允许用户添加的字段
		if (vk.pubfn.isNullOne(_id)) {
			return { code: -1, msg: '参数错误' };
		}
		let dbName = "tm-detection-form";
		let info = await vk.baseDao.findByWhereJson({
			dbName: "uni-id-users",
			whereJson: {
				nickname: detection_user,
			}
		}),
			_client_price = await vk.baseDao.findByWhereJson({
				dbName: "tm-client-price",
				whereJson: {
					client_id: client,
					category_id: detection_category
				}
			}),
			_price = 0,
			detection_user_sign = undefined;
		if (vk.pubfn.isNotNull(info) && vk.pubfn.isNotNull(info.sign_image)) {
			detection_user_sign = info.sign_image
		}

		await vk.baseDao.updateById({
			dbName,
			id: _id,
			dataJson: {
				client,
				client_name,
				detection_type,
				submitUser,
				detection_category,
				detection_standard,
				detection_standard_name,
				detection_user,
				detect_time,
						sample_id,
			sampleno,
			samplename,
				sample_save_info,
				sample_keeptime,
				sample_troptime,
				type,//是否溯源报告
				sample_status,
				detection_user_sign: detection_user_sign
			}
		});

		let _dels = sample_list.filter(x => x.isdel).map(x => x._id)
		await vk.baseDao.del({
			dbName: "tm-sample-testing",
			whereJson: {
				_id: _.in(_dels)
			}
		});
		if (sample_list.length > 0) {
			if (vk.pubfn.isNotNull(_client_price) && vk.pubfn.isNotNull(_client_price.price))
			_price = vk.pubfn.string2Number(_client_price.price);

			// 新增的记录（没有_id的）
			let _add_sample_list = sample_list.filter(x => vk.pubfn.isNull(x._id)).map(x => {
				return {
					'detectionform_id': _id,
					'client': client,
					'client_name': client_name,
					'detection_user': detection_user,
					'price': _price,
					...x
				}
			})
			if (vk.pubfn.isNotNull(_add_sample_list)) {
				await vk.baseDao.adds({
					dbName: "tm-sample-testing",// 表名
					dataJson: _add_sample_list
				});
			}

			// 更新已存在的记录（有_id且未删除的）
			let _update_sample_list = sample_list.filter(x => vk.pubfn.isNotNull(x._id) && !x.isdel);
			for (let i = 0; i < _update_sample_list.length; i++) {
				let item = _update_sample_list[i];
				let updateData = {
					'client': client,
					'client_name': client_name,
					'detection_user': detection_user,
					'price': _price,
					...item
				};
				// 移除_id字段，避免更新时出错
				delete updateData._id;
				await vk.baseDao.updateById({
					dbName: "tm-sample-testing",
					id: item._id,
					dataJson: updateData
				});
			}

		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}
